package edu.spec.chenli.Struts2;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public class LoginAction_1 implements Action, ServletResponseAware {
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private  String username;
    private  String password;
    private HttpServletResponse response;


    public void setServletResponse(HttpServletResponse response) {
        this.response=response;
    }
    public String execute()throws  Exception{
        ActionContext ac=ActionContext.getContext();
        Integer counter = (Integer) ac.getApplication().get("counter");

    if(counter==null) {
        counter=1;
    }
    else {
        counter=counter+1;
    }
    ac.getApplication().put("counter",counter);
    ac.getApplication().put("user",getUsername());
    if(getUsername().equals("abc")&&getPassword().equals("123")){
        Cookie c=new Cookie("user",getUsername());
        c.setMaxAge(60*60);
        response.addCookie(c);
        ac.put("tip","服务器提示：你已经成功第登录");
        return SUCCESS;
    }
    ac.put("tip","服务器提示：登录失败");
    return ERROR;
    }
}
